package com.ymatou.iapi.liveseller.testcase;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.ActivityProductCardBean;
import com.ymatou.iapi.liveseller.parameter.ActivityProductCardDto;
import com.ymatou.iapi.liveseller.parameter.ActivityProductCardResponse;
import com.ymatou.iapi.liveseller.parameter.ActivityProductDto;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.service.ActivityProductCardCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveActivity;
import com.ymttest.database.model.LiveActivityProduct;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*直播活动卡片
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ActivityProductCard{
	private static ActivityProductCardBean activityproductcardBean;
	private static ActivityProductCardCall activityproductcardCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	LiveRoomWapper LiveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("直播活动卡片");
	}

	@Before
	public void caseUp() {
		activityproductcardBean=new ActivityProductCardBean();
		activityproductcardCall=new ActivityProductCardCall();
		LiveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(activityproductcardCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(AddSellerProductRequest addpro, ActivityProductCardResponse activityProductCardResponse) throws InterruptedException {
		Logger.verifyEquals(activityproductcardBean.getLiveActivityId(), activityProductCardResponse.getLiveActivityId(), "验证LiveActivityId成功");
		LiveRoomWapper LiveRoomWapper = new LiveRoomWapper();
		Integer liveacctivityid =activityProductCardResponse.getLiveActivityId();
		LiveActivity activity = LiveRoomWapper.selectLiveactivityById(liveacctivityid);
		int state = getActivityStatus(new Date(activityProductCardResponse.getStartTime()), new Date(activityProductCardResponse.getEndTime()));
		Logger.verifyEquals(state, activityProductCardResponse.getActivityState(), "验证ActivityState成功");
		//目前传参的话最多只会有1个
		Logger.verifyNotNull(activityProductCardResponse.getProductCount(), "验证ProductCount");
		// 可能有误差
		Logger.verifyEquals(Long.valueOf(activityProductCardResponse.getStartTime()) / 10000, activity.getStartTime().getTime() / 10000, "验证StartTime");
		Logger.verifyEquals(Long.valueOf(activityProductCardResponse.getEndTime()) / 10000, activity.getEndTime().getTime() / 10000, "验证EndTime");
		activityProductCardResponse.getActivityProducts().forEach(ap -> {
			try {
				LiveActivityProduct aproduct = LiveRoomWapper.selectLiveactivityproduct(liveacctivityid, ap.getProductId()).get(0);
				Logger.verifyEquals( aproduct.getProductId(),ap.getProductId(), "验证ProductId");
				Logger.verifyEquals(addpro.getProduct().getProductName(),ap.getProductName(), "验证ProductId");
				Logger.verifyEquals(addpro.getProduct().getProductPicList().get(0),ap.getMainPic(), "验证MainPic");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		});

	}
	/**
	 * 根据开始和结束时间来判断当前直播的状态
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	private Integer getActivityStatus(Date start, Date end) {
		long now = System.currentTimeMillis();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(start);
		calendar.add(Calendar.SECOND, -30 * 60);
		long lockTime = calendar.getTime().getTime();
		if (now < lockTime) {
			return 4;
		} else if (now >= lockTime && now < start.getTime()) {
			return 3;
		} else if (now >= start.getTime() && now < end.getTime()) {
			return 2;
		} else if (now >= end.getTime()) {
			return 1;
		}
		return 0;
	}

	/**
	 * 根据直播状态获取回显字符
	 * 
	 * @param ActivityState
	 * @return
	 */
	private String getActivityStateText(Integer ActivityState) {
		String text = "";
		// 1已结束 2进行中 3即将开始 4 未开始
		if (ActivityState == 1) {
			text = "已结束";
		} else if (ActivityState == 2) {
			text = "进行中";
		} else if (ActivityState == 3) {
			text = "即将开始";
		} else if (ActivityState == 4) {
			text = "未开始";
		}
		return text;
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ActivityProductCard_001() {
		Logger.start(true,"查询直播活动卡片,成功");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 60 * 60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			Integer liveacctivityid = LiveRoomWapper.selectLiveactivityByLiveId(createliveactivityBean.getLiveActivity().getLiveId()).get(0).getLiveActivityId();
			activityproductcardBean.setLiveActivityId(liveacctivityid);
			activityproductcardCall.setData(activityproductcardBean);
			activityproductcardCall.callService();			
			Logger.verifyEquals("200", activityproductcardCall.getString("Code"), "验证成功");
			verifyDB(addpro,activityproductcardCall.getData());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_ActivityProductCard_002() {
		Logger.start(true,"不传参数失败");
		try {
			activityproductcardCall.setData(activityproductcardBean);
			activityproductcardCall.callService();			
			Logger.verifyEquals("400", activityproductcardCall.getString("Code"), "验证成功");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}